home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Games / NeXTGo / Source / godict.c < prev    next >
C/C++ Source or Header  |  1993-02-08  |  5KB  |  244 lines

  1. #include "comment.header"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "godict.h"
  7.  
  8. #define LANGENTRY(line,label)    (!strncmp(line,label,strlen(label)))
  9.  
  10. #ifndef _TEST_COMPILE_
  11. extern int NXRunAlertPanel(const char *title, const char *msg, const char *defaultButton, const char *alternateButton, const char *otherButton, ...);
  12. #endif
  13.  
  14. #ifdef _DEBUG_DICT_
  15. FILE *dump;
  16. #endif
  17.  
  18. GODICT* load_dict(char* filename)
  19. {
  20.   FILE *godictfile;
  21.   GODICT *d, *dp, *d_old;
  22.   char line[MAXDICTLINE];
  23.   int linenr = 0;
  24.  
  25.   dp = NULL;
  26.   d = NULL;
  27.  
  28. #ifdef _DEBUG_DICT_
  29.   dump = fopen("debug.dict","w");
  30. #endif
  31.  
  32.   if ((godictfile = fopen(filename,"r")) == NULL)
  33.     {
  34.       return NULL;
  35.     }
  36.  
  37.   while (fgets(line, MAXDICTLINE, godictfile) != NULL)
  38.     {
  39.       char *newline;
  40.  
  41.       linenr++;
  42.       if ((newline = strchr(line, '\n')) != (char *)0) *newline = 0;
  43.       if ((newline = strchr(line, '\r')) != (char *)0) *newline = 0;
  44.  
  45.       if ((*line == 0) || (*line == COMMENT)) continue;
  46.  
  47.       if (strlen(line) < strlen(RD_CD))
  48.     {
  49.       char s[80];
  50.  
  51.       sprintf(s, "There is a bad entry on line %d.", linenr);
  52. #ifndef _TEST_COMPILE_
  53.       NXRunAlertPanel("NeXTGo Dictionary", s, "OK", 0, 0);
  54. #endif
  55.     }
  56.       else if (strncmp(line, RD_CD, strlen(RD_CD)) == 0)
  57.     {
  58.       d_old = d;
  59.       d = (GODICT *) malloc(sizeof(GODICT));
  60.       d->dct_jp = NULL;
  61.       d->dct_ch = NULL;
  62.       d->dct_rk = NULL;
  63.       d->dct_gb = NULL;
  64.       d->dct_nl = NULL;
  65.       d->dct_ge = NULL;
  66.       d->dct_fr = NULL;
  67.       d->dct_dg = NULL;
  68.       d->dct_cp = NULL;
  69.  
  70.       if (dp == NULL)
  71.         {
  72.           dp = d;
  73.         }
  74.       else
  75.         {
  76.           d_old->dct_next = d;
  77.         }
  78.  
  79.       switch (*(line+strlen(RD_CD)))
  80.         {
  81.         case 'n':  d->dct_type = CD_NAME;
  82.           break;
  83.         case 'c':  d->dct_type = CD_CHAM;
  84.           break;
  85.         case 't':  d->dct_type = CD_TECH;
  86.           break;
  87.         case 'p':  d->dct_type = CD_POLI;
  88.           break;
  89.         case 'd':  d->dct_type = CD_DIGI;
  90.           break;
  91.         default:  d->dct_type = CD_MISC;
  92.           break;
  93.         }
  94.     }
  95.       else if LANGENTRY(line,RD_JP)
  96.     {
  97.       store_dict(&(d->dct_jp), line+strlen(RD_JP));
  98.     }
  99.       else if LANGENTRY(line,RD_CH)
  100.     {
  101.       store_dict(&(d->dct_ch), line+strlen(RD_CH));
  102.     }
  103.       else if LANGENTRY(line,RD_RK)
  104.     {
  105.       store_dict(&(d->dct_rk), line+strlen(RD_RK));
  106.     }
  107.       else if LANGENTRY(line,RD_GB)
  108.     {
  109.       store_dict(&(d->dct_gb), line+strlen(RD_GB));
  110.     }
  111.       else if LANGENTRY(line,RD_NL)
  112.     {
  113.       store_dict(&(d->dct_nl), line+strlen(RD_NL));
  114.     }
  115.       else if LANGENTRY(line,RD_GE)
  116.     {
  117.       store_dict(&(d->dct_ge), line+strlen(RD_GE));
  118.     }
  119.       else if LANGENTRY(line,RD_FR)
  120.     {
  121.       store_dict(&(d->dct_fr), line+strlen(RD_FR));
  122.     }
  123.       else if LANGENTRY(line,RD_DG)
  124.     {
  125.       store_dict(&(d->dct_dg), line+strlen(RD_DG));
  126.     }
  127.       else if LANGENTRY(line,RD_CP)
  128.     {
  129.       store_dict(&(d->dct_cp), line+strlen(RD_CP));
  130.     }
  131.  
  132.     }
  133.  
  134.   fclose(godictfile);
  135.   return dp;
  136. }
  137.  
  138. void store_dict(char **f, char *s)
  139. {
  140.   int more = (*f != NULL);
  141.   long needed = strlen(s) + 1;
  142.  
  143.   if (!*s) return;
  144.  
  145.   if (more)
  146.     {
  147.       needed += strlen(*f) + 1;
  148.       *f = (char *) realloc(*f, needed);
  149.     }
  150.   else
  151.     {
  152.       *f = (char *) malloc(needed);
  153.     }
  154.  
  155.   if (more)
  156.     {
  157.       strcat(*f, "\n");
  158.       strcat(*f, lstr(s));
  159.     }
  160.   else
  161.     {
  162.       strcpy(*f, lstr(s));
  163.     }
  164. #ifdef _DEBUG_DICT_
  165.   fprintf(dump,"Added:  %s",s);
  166. #endif
  167. }
  168.  
  169. char* lstr(char *s)
  170. {
  171.   char *t;
  172.  
  173.   for (t = s; *t; t++)
  174.     if (isupper(*t))
  175.       *t = tolower(*t);
  176.  
  177.   return s;
  178. }
  179.  
  180. int substr(char s[], char sub_s[])
  181. {
  182.   int i, j, k;
  183.  
  184.   if (s == NULL || sub_s == NULL)
  185.     return 0;
  186.  
  187.   if (strlen(s) < strlen(sub_s))
  188.     return 0;
  189.  
  190.   for (i = 0; i < strlen(s) - strlen(sub_s); i++)
  191.     {
  192.       k = 1;
  193.       for (j = 0; (j < strlen(sub_s)) && k; j++)
  194.     {
  195.       if (sub_s[j] != s[i+j]) k = 0;
  196.     }
  197.       if (k)
  198.     {
  199.       return 1;
  200.     }
  201.     }
  202.  
  203.   return 0;
  204. }
  205.  
  206. int termtypes, languages;
  207.  
  208. GODICT* search_dict(GODICT* gd, char* term)
  209. {
  210.   GODICT *d;
  211.   char s[80];
  212.  
  213.   d = gd;
  214.   term = lstr(term);
  215.   sprintf(s,"Starting search for %s.",term);
  216.   while (d != NULL)
  217.     {
  218.       if (d->dct_type & termtypes)
  219.     {
  220.       if ((languages & (LANG_JP)) && d->dct_jp && strstr(d->dct_jp,term))
  221.         return d;
  222.       if ((languages & (LANG_CH)) && d->dct_ch && strstr(d->dct_ch,term))
  223.         return d;
  224.       if ((languages & (LANG_RK)) && d->dct_rk && strstr(d->dct_rk,term))
  225.         return d;
  226.       if ((languages & (LANG_GB)) && d->dct_gb && strstr(d->dct_gb,term))
  227.         return d;
  228.       if ((languages & (LANG_NL)) && d->dct_nl && strstr(d->dct_nl,term))
  229.         return d;
  230.       if ((languages & (LANG_GE)) && d->dct_ge && strstr(d->dct_ge,term))
  231.         return d;
  232.       if ((languages & (LANG_FR)) && d->dct_fr && strstr(d->dct_fr,term))
  233.         return d;
  234.       if ((languages & (LANG_DG)) && d->dct_dg && strstr(d->dct_dg,term))
  235.         return d;
  236.       if ((languages & (LANG_CP)) && d->dct_cp && strstr(d->dct_cp,term))
  237.         return d;
  238.     }
  239.       d = d->dct_next;
  240.     }
  241.  
  242.   return NULL;
  243. }
  244.